import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import { Button } from 'antd';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import ListTable from "./Components/ListTable";
import RepositoryMove from "./Components/RepositoryMove";
import RepositoryAdd from "./Components/RepositoryAdd";



@connect()

export default class EquipmentList extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      modalRepositoryAddVisible: false,
      modalRepositoryMoveVisible: false,
      modalMoveids:null,
      isReloadList:false,
    };
  }


  componentWillUnmount(){
    const { dispatch } = this.props;
    dispatch({
      type: `repository/appendClearState`,
    })
  }

  componentDidMount() {
    // const { dispatch } = this.props;
    // 数据字典

  }


  //
  sotpReloadList = () =>{
    this.setState({
      isReloadList:false,
    });
  }

  // 移动
  handleRepositoryMove = (ids) => {
    console.log(ids)
    this.setState({
      modalMoveids: ids,
    },()=>{
      this.handleRepositoryMoveModalVisible(true);
    });
  };

  // 显示移动model
  handleRepositoryMoveModalVisible = flag => {
    this.setState({
      modalRepositoryMoveVisible: !!flag,
    });
  };

  // 移动model回调
  handleRepositoryMoveModelCallBack=(value)=>{
    this.handleRepositoryMoveModalVisible(false);
    const { dispatch } = this.props;
    const { modalMoveids } = this.state;

    if(value && modalMoveids && modalMoveids.length>0){
      dispatch({
        type: 'repository/action_repository_move',
        payload: {
          repositoryId:modalMoveids.join(","),
          ...value,
        },
      }).then(()=>{
        this.setState({
          selectedRows: [],
          isReloadList:true,
        });
      });
    }

  }

  // 显示新增修改model
  handleRepositoryModalVisible= flag => {
    this.setState({
      modalRepositoryAddVisible: !!flag,
    });
  };

  // 设备新增、编辑后model回调
  handleRepositoryModelCallBack=()=>{
    this.handleRepositoryModalVisible(false);
    this.setState({
      isReloadList:true,
    });
  }

  handleAddRepository = e => {
    this.handleRepositoryModalVisible(true);
  };



  render() {
    const { dictionaries, selectSource } = this.props;
    const { modalRepositoryAddVisible, modalRepositoryMoveVisible, isReloadList } = this.state;

    const location_arry = location.pathname.split("/");
    const tabDefaultKeys = location_arry[location_arry.length - 1];
    const fileFolder = (tabDefaultKeys=="1" || tabDefaultKeys=="2" || tabDefaultKeys=="3" || tabDefaultKeys=="4" || tabDefaultKeys=="5" || tabDefaultKeys=="6") ? tabDefaultKeys : "6"  ;
    const floderList =[{
        value: '1',
        label: '规范标准',
      },
      {
        value: '2',
        label: '规则制度',
      },
      {
        value: '3',
        label: '计划文档',
      },
      {
        value: '4',
        label: '报告文档',
      },
      {
        value: '5',
        label: '草案文档',
      },
      {
        value: '6',
        label: '其他',
      }]


    return (

          <PageHeaderLayout >
            <ListTable
              dictionaries={dictionaries}
              selectSource={selectSource}
              addModelCallBack={this.handleAddRepository}
              repositoryMoveModelCallBack={this.handleRepositoryMove}
              isReloadList={isReloadList}
              sotpReloadList={this.sotpReloadList}
              fileFolder={fileFolder}
            />

            <RepositoryAdd
              visible={modalRepositoryAddVisible}
              onCancel={e => this.handleRepositoryModalVisible(e)}
              callback={this.handleRepositoryModelCallBack}
              fileFolder={fileFolder}
            />

            <RepositoryMove
              visible={modalRepositoryMoveVisible}
              onCancel={e => this.handleRepositoryMoveModalVisible(e)}
              callback={this.handleRepositoryMoveModelCallBack}
              floderList={floderList}
              fileFolder={fileFolder}
            />


          </PageHeaderLayout>


    );
  }
}
